import pygame, sys, math

from HealthBar import HB
from Enemy import Enemy

class Player(pygame.sprite.Sprite):
	def __init__(self, pos = (0,0), blocksize = [50,50], screensize = [800,600]):
		pygame.sprite.Sprite.__init__(self, self.containers)
		self.screensize = screensize
		self.standRightImages = [pygame.image.load("rsc/player/Male/playerBall.png")]
		self.standRightImages = [pygame.transform.scale(self.standRightImages[0], blocksize)]
		self.standLeftImages = [pygame.transform.flip(self.standRightImages[0], 1,0)]
		self.rightImages = [pygame.image.load("rsc/player/Male/playerBall_right1.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right2.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right3.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right4.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right5.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right6.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right7.png"),
						 pygame.image.load("rsc/player/Male/playerBall_right8.png")]
		self.rightImages = [pygame.transform.scale(self.rightImages[0], blocksize),
						 pygame.transform.scale(self.rightImages[1], blocksize),
						 pygame.transform.scale(self.rightImages[2], blocksize),
						 pygame.transform.scale(self.rightImages[3], blocksize),
						 pygame.transform.scale(self.rightImages[4], blocksize),
						 pygame.transform.scale(self.rightImages[5], blocksize),
						 pygame.transform.scale(self.rightImages[6], blocksize),
						 pygame.transform.scale(self.rightImages[7], blocksize)]
		self.leftImages = [pygame.image.load("rsc/player/Male/playerBall_left1.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left2.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left3.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left4.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left5.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left6.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left7.png"),
						 pygame.image.load("rsc/player/Male/playerBall_left8.png")]
		self.leftImages = [pygame.transform.scale(self.leftImages[0], blocksize),
						 pygame.transform.scale(self.leftImages[1], blocksize),
						 pygame.transform.scale(self.leftImages[2], blocksize),
						 pygame.transform.scale(self.leftImages[3], blocksize),
						 pygame.transform.scale(self.leftImages[4], blocksize),
						 pygame.transform.scale(self.leftImages[5], blocksize),
						 pygame.transform.scale(self.leftImages[6], blocksize),
						 pygame.transform.scale(self.leftImages[7], blocksize)]
		self.shootRightImages = [pygame.image.load("rsc/player/Male/attack/ewa1.png"),
						 pygame.image.load("rsc/player/Male/attack/ewa2.png"),
						 pygame.image.load("rsc/player/Male/attack/ewa3.png"),
						 pygame.image.load("rsc/player/Male/attack/ewa4.png"),
						 pygame.image.load("rsc/player/Male/attack/ewa5.png")]
		self.shootRightImages = [pygame.transform.scale(self.shootRightImages[0], blocksize),
						 pygame.transform.scale(self.shootRightImages[1], blocksize),
						 pygame.transform.scale(self.shootRightImages[2], blocksize),
						 pygame.transform.scale(self.shootRightImages[3], blocksize),
						 pygame.transform.scale(self.shootRightImages[4], blocksize)]
		self.shootLeftImages = [pygame.transform.flip(self.shootRightImages[0], 1,0),
						pygame.transform.flip(self.shootRightImages[1], 1,0),
						pygame.transform.flip(self.shootRightImages[2], 1,0),
						pygame.transform.flip(self.shootRightImages[3], 1,0),
						pygame.transform.flip(self.shootRightImages[4], 1,0)]
		self.shootLeftImages = [pygame.transform.scale(self.shootLeftImages[0], blocksize),
						 pygame.transform.scale(self.shootLeftImages[1], blocksize),
						 pygame.transform.scale(self.shootLeftImages[2], blocksize),
						 pygame.transform.scale(self.shootLeftImages[3], blocksize),
						 pygame.transform.scale(self.shootLeftImages[4], blocksize)]
		self.jumpImages = [pygame.image.load("rsc/player/Male/playerBall.png")]
		self.jumpImages = [pygame.transform.scale(self.standRightImages[0], blocksize)]
		
					
		self.images = self.standRightImages
		self.frame = 0
		self.maxFrame = len(self.images) - 1
		self.waitCount = 0
		self.waitCountMax = 1
		self.image = self.images[self.frame]
		self.rect = self.image.get_rect()
		self.maxSpeed = blocksize[0]/11.0
		self.speed = [0,0]
		self.speedx = 0
		self.speedy = 0
		self.health = 100
		self.jumpSpeed = 0
		self.jumpSpeedMax = 25
		self.g = blocksize[0]/10
		self.fallSpeedMax = int(blocksize[0]/2) -3
		self.realx = pos[0]
		self.realy = pos[1]
		self.x = screensize[0]/2
		self.y = screensize[1]/2
		self.offsetx = self.x - self.realx
		self.offsety = self.y - self.realy
		self.scrollingx = False
		self.scrollingy = False
		self.scrollBoundry = 200
		self.headingx = "right"
		self.headingy = "up"
		self.lastHeading = "right"
		self.money = 0
		self.headingChanged = False
		self.radius = self.rect.width/2
		self.living = True
		self.place(pos)
		self.shooting = False
		self.onfloor = False
		self.floor = screensize[1]
		self.touchFloor = False
		
	def die(self):
		self.health = 0
		
	def life(self):
		self.health = 100
		
	def place(self, pos):
		self.rect.center = pos
		
	def fixLocation(self, x, y):
		pass
		
	def collideMoney(self, money):
		self.money += money.value		 
		
	def update(*args):
		self = args[0]
		self.collideWall(self.screensize)
		if self.shooting:
			if self.frame == self.maxFrame:		#end of shoot animation
				self.shooting = False
				self.lastHeading = "stop " + self.lastHeading[6:] #remove 'blast ' for last heading
				self.headingChanged = True
		self.animate()
		if (self.rect.bottom < self.floor) and self.headingy == "none":
			self.headingy = "down"
		self.move()
		self.headingChanged = False
		self.touchFloor = False
		if self.health <= 0:
			self.living = False
		
		 
	def damageTaken(self, enemy):
		self.health -= enemy.damage
		#print "???????????????", self.health
		if self.health <= 0:
			self.kill()
			
	def animate(self):
		if self.headingChanged:
			if self.lastHeading == "up":
				self.images = self.jumpImages
			if self.lastHeading == "down":
				self.images = self.jumpImages
			if self.lastHeading == "right":
				self.images = self.rightImages
				self.maxFrame = len(self.images) - 1
			if self.lastHeading == "stop right":
				self.images = self.standRightImages
				self.maxFrame = len(self.images) - 1
			if self.lastHeading == "blast right":
				self.images = self.shootRightImages
				self.maxFrame = len(self.images) - 1
			if self.lastHeading == "blast left":
				self.images = self.shootLeftImages
				self.maxFrame = len(self.images) - 1
			if self.lastHeading == "left":
				self.images = self.leftImages
				self.maxFrame = len(self.images) - 1
			if self.lastHeading == "stop left":
				self.images = self.standLeftImages
				self.maxFrame = len(self.images) - 1
			self.frame = 0
			self.maxFrame = len(self.images) - 1
			self.image = self.images[self.frame]
			
		if self.waitCount < self.waitCountMax:
			self.waitCount += 1
		else:
			self.waitCount = 0
			if self.frame < self.maxFrame:
				self.frame += 1
			else:
				self.frame = 0
			self.image = self.images[self.frame]
			
	def collideEnemy(self,enemy):
		self.health -= enemy.damage
		
		
	def move(self):
		self.realx += self.speedx
		self.realy += self.speedy
		
		if not self.touchFloor:
			self.headingy = "down"
		if self.headingy == "down":
			if self.speedy < self.fallSpeedMax:
				self.speedy += self.g
			else:
				self.speedy = self.fallSpeedMax
		
		if not self.scrollingx:
			self.x += self.speedx
		if self.x > self.screensize[0] - self.scrollBoundry and self.headingx == "right":
			self.scrollingx = True
		elif self.x < self.scrollBoundry and self.headingx == "left":
			self.scrollingx = True
		else:
			self.scrollingx = False
			
		if not self.scrollingy:
			self.y += self.speedy
		if self.y > self.screensize[1] - self.scrollBoundry and self.headingy == "down":
			self.scrollingy = True
		elif self.y < self.scrollBoundry and self.headingy == "up":
			self.scrollingy = True
		else:
			self.scrollingy = False
						
		self.rect.center = (round(self.x), round(self.y))
		
	def collideWall(self, size):
		if self.rect.left < 0 and self.headingx == "left":
			self.speedx = 0
		elif self.rect.right > size[0] and self.headingx == "right":
			self.speedx = 0
		if self.rect.top < 0 and self.headingy == "up":
			self.speedy = 0
		elif self.rect.bottom > size[1] and self.headingy == "down":
			self.speedy = 0
			
			
	def collideEnemy(self, other):
		self.health -= other.damage
	
	def collideBlock(self, block):
		#print self.rect, self.headingx, self.headingy
		if self.floor == block.rect.top + 2 and self.headingy == "none":
			self.touchFloor = True
			self.jumping = False
		else:
			if self.realx < block.realx and self.headingx == "right":
				self.speedx = 0
				self.realx -= 1
				self.x -= 1
			if self.realx > block.realx and self.headingx == "left":
				self.speedx = 0
				self.realx += 1
				self.x += 1
			if self.realy < block.realy and self.headingy == "up":
				self.speedy = 0
				self.realy += 1
				self.y += 1
				#print "hit up"
			if self.realy > block.realy and self.headingy == "down":
				self.touchFloor = True
				self.speedy = 0
				self.realy -= self.g + 2
				self.headingy = "none"
				self.floor = block.rect.top+2
				self.y = self.floor - self.rect.height/2
   
	def direction(self, dir):
		if dir == "right":
			self.headingx = "right"
			self.speedx = self.maxSpeed
			self.lastHeading = "right"
			self.headingChanged = True
		if dir == "stop right":
			self.headingx = "right"
			self.lastHeading = "stop right"
			self.headingChanged = True
			self.speedx = 0
		if dir == "left":
			self.headingx = "left"
			self.speedx = -self.maxSpeed
			self.lastHeading = "left"
			self.headingChanged = True
		if dir == "stop left":
			self.headingx = "left"
			self.lastHeading = "stop left"
			self.headingChanged = True
			self.speedx = 0
		if dir == "blast right":
			self.lastHeading = "blast right"
			self.headingChanged = True
			self.shooting = True
			self.waitCountMax = 3
			self.waitCount = 0
			self.frame = 0
		if dir == "jump":
			if not self.jumping:
				self.jumping = True
				self.headingy = "up"
				self.jumpSpeed = self.jumpSpeedMax
				self.speedy = -self.jumpSpeed
				self.headingChanged = "up"
				self.touchingFloor = False
				if self.lastHeading == "right":
					self.speedx = self.maxSpeed
				elif self.lastHeading == "left":
					self.speedx = -self.maxSpeed
		if dir == "up":
			self.headingy = "up"
			self.speedy = -self.maxSpeed
			self.lastHeading = "up"
			self.headingChanged = True
		if dir == "stop up":
			self.headingy = "up"
			self.speedy = 0
		if dir == "down":
			self.headingy = "down"
			self.speedy = self.maxSpeed
			self.lastHeading = "down"
			self.headingChanged = True
		if dir == "stop down":
			self.headingy = "down"
			self.speedy = 0
		if dir == "blast left":
			self.lastHeading = "blast left"
			self.headingChanged = True
			self.shooting = True
			self.waitCountMax = 3
			self.waitCount = 0
			self.frame = 0